<?php

/**
 * result() 返回状态码，信息，数据
 * @param  int  $code   [可选]状态码：
 *                      20000 成功
 *                      40001 未授权
 * @param  string  $msg   [可选]操作的提示
 * @param  array   $extra [可选]额外的信息
 * @return array          操作结果
 */
function result($code = 20000, $status = 1, $msg = '', array $extra = array()){
    return response()->json(['code'=>$code, 'status' => $status, 'msg'=>$msg, 'data'=>$extra]);
}

/**
 * 友好时间显示
 * @param $time
 * @return bool|string
 */
 function friendDate($time)
 {
     if (!$time)
         return false;
     $fdate = '';
     $d = time() - intval($time);
     $ld = $time - mktime(0, 0, 0, 0, 0, date('Y')); //得出年
     $md = $time - mktime(0, 0, 0, date('m'), 0, date('Y')); //得出月
     $byd = $time - mktime(0, 0, 0, date('m'), date('d') - 2, date('Y')); //前天
     $yd = $time - mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')); //昨天
     $dd = $time - mktime(0, 0, 0, date('m'), date('d'), date('Y')); //今天
     $td = $time - mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')); //明天
     $atd = $time - mktime(0, 0, 0, date('m'), date('d') + 2, date('Y')); //后天
     if ($d == 0) {
         $fdate = '刚刚';
     } else {
         switch ($d) {
             case $d < $atd:
                 $fdate = date('Y年m月d日', $time);
                 break;
             case $d < $td:
                 $fdate = '后天' . date('H:i', $time);
                 break;
             case $d < 0:
                 $fdate = '明天' . date('H:i', $time);
                 break;
             case $d < 60:
                 $fdate = $d . '秒前';
                 break;
             case $d < 3600:
                 $fdate = floor($d / 60) . '分钟前';
                 break;
             case $d < $dd:
                 $fdate = floor($d / 3600) . '小时前';
                 break;
             case $d < $yd:
                 $fdate = '昨天' . date('H:i', $time);
                 break;
             case $d < $byd:
                 $fdate = '前天' . date('H:i', $time);
                 break;
             case $d < $md:
                 $fdate = date('m月d日 H:i', $time);
                 break;
             case $d < $ld:
                 $fdate = date('m月d日', $time);
                 break;
             default:
                 $fdate = date('Y年m月d日', $time);
                 break;
         }
     }
     return $fdate;
 }

 /**
  * 手机号中间用*星号隐藏
  */
 function mobile_asterisk($mobile){
    return $mobile_asterisk = substr($mobile, 0, 4) . '****' . substr($mobile, 8, 3);
 }

  /**
  * 省份证号中间用*星号隐藏
  */
  function idcard_asterisk($idcard){
    return strlen($idcard) ==15 ? substr_replace($idcard, '****', 8, 4) 
    : (strlen($idcard) == 18 ? substr_replace($idcard, '****************', 1, 16) : $idcard);
 }

   /**
  * 省份证号中间用*星号隐藏
  */
  function bankcard_asterisk($bankcard){
    return strlen($bankcard) ==16 ? substr_replace($bankcard, '****', 4, 8) 
    : (strlen($bankcard) == 19 ? substr_replace($bankcard, '****************', 4, 11) : $bankcard);
 }